home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / qdeck / sockets / udaemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  2.5 KB  |  90 lines

  1. /*****************************************************************************
  2. *  
  3. *                                  DESQview/X 
  4. *                           BASIC UDP (DATAGRAM) DAEMON
  5. *
  6. *  The following code demonstrates the implementation of a basic stream 
  7. *  daemon under DESQview/X.  Similar code may be used for daemons that are
  8. *  managed by the DESQview/X Network Manager and are activated by  
  9. *  incoming datagrams from a remote host.
  10. *
  11. *  The basic methodology is as follows.  
  12. *
  13. *   - The DESQview/X Network Manager opens a datagram socket for the daemon
  14. *     based upon the contents of the NETWORK\INETD.CFG file, and the DVPs in 
  15. *     the NETWORK subdirectory.
  16. *
  17. *   - A datagram is sent to the socket.
  18. *
  19. *   - The Network Manager instructs DESQview/X to load the appropriate 
  20. *     DVP for the daemon (from INETD.CFG).
  21. *
  22. *   - The daemon is started and immediately requests information about the 
  23. *     socket.
  24. *
  25. *   - The daemon then processes the datagram.
  26. *
  27. *     - The daemon then instructs the Network Manager that it is finished
  28. *        with the socket and relinquishes control of it to the Network Manager.
  29. *
  30. *
  31. ******************************************************************************/
  32.  
  33.  
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37.  
  38. #include <netdb.h>        
  39. #include <netinet\in.h>    
  40. #include <sys\time.h>
  41. #include <sys\socket.h>
  42. #include <sys\errno.h>
  43. #include <sys\ioctl.h>
  44.  
  45.  
  46. void    chat(int s);
  47.  
  48. void main(int argc, char *argv[])
  49. {
  50.     int                        s,bytes;
  51.     char                        rbuff[81];
  52.     struct sockaddr_in    source;
  53.     int                        len    = sizeof(struct sockaddr_in);
  54.  
  55.     /* Daemon has been started by the Network Manager.  Request the connection */
  56.     /* information.  The second parameter to so_daemon indicates that we wish    */
  57.     /* to wait for the connection information.  If we wished to poll for the    */
  58.     /* next connection, we could simply specify FALSE in the second parameter.    */
  59.  
  60.     printf("\n\nDESQview/X Sample DATAGRAM Daemon.\n");
  61.  
  62.     while(1){
  63.  
  64.         printf("\n\nWaiting for next datagram.  Please wait.\n");
  65.  
  66.         s    =    so_daemon(argv,1);
  67.  
  68.         if(s < 0){
  69.             printf("\nError:  Unable to retrieve connection information.\n\n");
  70.             break;
  71.         }
  72.  
  73.         /* Get the datagram                                                        */
  74.  
  75.         len    = sizeof(struct sockaddr_in);
  76.  
  77.         bytes    = recvfrom(s, rbuff, 80 , 0, (struct sockaddr *)&source, &len);
  78.  
  79.         if(bytes > 0)
  80.             
  81.             printf("\n%s -> %s",inet_ntoa(source.sin_addr), rbuff);
  82.  
  83.         /* Relinquish control of the socket to the Network Manager.    */
  84.  
  85.         so_unlink(s);
  86.  
  87.     }
  88. }
  89.  
  90.